﻿using System.Collections.Generic;
using Castle.Facilities.NHibernateIntegration;
using NHibernate;
using System.Data;
using System;

namespace Inovout.Repositories
{
    public class DatabaseRepository
    {
        protected ISessionManager sessionManager;
        private string databaseName;
        public DatabaseRepository(string databaseName)
        {
            this.databaseName = databaseName;
            sessionManager = ComponentRegistry.Resolve<ISessionManager>();
        }

        public T[] GetDatas<T>(string queryStatement,Func<IDataReader,T> fillDataFunc)
        {
            List<T> list = new List<T>();
            using (IStatelessSession session = sessionManager.OpenStatelessSession(databaseName))
            {
                IDbCommand command = session.Connection.CreateCommand();
                command.CommandText = queryStatement;
                using (IDataReader dataReader = command.ExecuteReader())
                {
                    while (dataReader.Read())
                    {
                        list.Add(fillDataFunc(dataReader));
                    }
                }
            }

            return list.ToArray();
        }
    }
}
